/**
 * Copyright (c) 2008, Mu Dynamics.
 *  All rights reserved.
 *  
 *   Redistribution and use in source and binary forms, with or without modification, 
 *   are permitted provided that the following conditions are met:
 *   
 *  - Redistributions of source code must retain the above copyright notice, 
 *     this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice, 
 *     this list of conditions and the following disclaimer in the documentation and/or 
 *     other materials provided with the distribution.
 *  - Neither the name of the "Mu Dynamics" nor the names of its contributors may be used 
 *     to endorse  or promote products derived from this software without specific prior 
 *     written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.mu.jacob.core.ant;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.types.Reference;

import com.google.inject.Binder;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.mu.jacob.core.ant.ConfigModule.BuilderFctory;
import com.mu.jacob.core.builder.IBuilder;
import com.mu.jacob.core.generator.Config;
import com.mu.jacob.core.generator.DecoratorModule;
import com.mu.jacob.core.generator.DefaultConfig;
import com.mu.jacob.core.generator.ElementModule;
import com.mu.jacob.core.generator.JacobProcessor;

/**
 * Main Jacob task, initializes and runs Jacob processor
 * @author Adam Smyczek
 */
public class JacobTask extends GeneratorTask {

	/* Model type containing all model classes */
	private ModelType modelType = null;
	
	/* Builder task list */
	private final List<BuilderTask> builderTasks = new ArrayList<BuilderTask>();

	/* Custom module for DI */
	private String module = null;
	
	/**
	 * @return create model task, only one can be configured
	 */
	public ModelType createModel() {
		if (modelType != null) {
			throw new BuildException("Only a single descriptor configuration is allowed.");
		}
		modelType = new ModelType();
		return modelType;
	}

	/**
	 * @return custom builder task
	 */
	public BuilderTask createBuilder() {
		return addBuilderTask(new CustomBuilderTask());
	}

	/**
	 * @return file builder task
	 */
	public BuilderTask createFileBuilder() {
		return addBuilderTask(new DefaultBuilderTask(BuilderTask.BuilderType.FILE));
	}

	/**
	 * @return element builder task
	 */
	public BuilderTask createElementBuilder() {
		return addBuilderTask(new DefaultBuilderTask(BuilderTask.BuilderType.ELEMENT));
	}
	
	/**
	 * Internal add builder task method
	 * @param builderTask
	 * @return the builder task
	 */
	private BuilderTask addBuilderTask(BuilderTask builderTask) {
		builderTasks.add(builderTask);
		return builderTask;
	}
	
	/**
	 * Set descriptor task as reference
	 * @param model reference called by ant
	 */
    public void setModelRef(Reference reference) {
    	Object refObject = reference.getReferencedObject(getProject());
    	if (refObject instanceof ModelType) {
    		if (modelType != null) {
    			throw new BuildException("Only a single model configuration is allowed.", getLocation());
    		} else {
    			modelType = (ModelType)refObject;
    		}
    	} else {
    		throw new BuildException("Referenced model is not a model type!", getLocation());
    	}
    }
    
    /**
     * Sets custom module used for dependency injection
     * @param module
     */
	public void setModule(String module) {
		this.module = module;
	}
	
    /**
     * Main execute method
     */
	@Override
	public void execute() throws BuildException {

		// validate configuration
		logger.debug("Validating configuration");
		validateConfiguration();

		try {
			// Configure class loader
			setThreadContextLoader();
			
			// Bootstrap jacob
			logger.debug("Bootstrapping application");
			DecoratorModule decoratorModule = new DecoratorModule();
			ConfigModule configModule = new ConfigModule();
			ElementModule elementModule = (modelType.getElementModule() == null)? new ElementModule() :
												newInstance(modelType.getElementModule(), ElementModule.class);
			Module customModule = (module == null)? emptyModule : newInstance(module, Module.class);
			Injector injector = Guice.createInjector(configModule, decoratorModule, elementModule, customModule);
			
			// Create configuration
			logger.debug("Configuring jacob");
			DefaultConfig config = (DefaultConfig)injector.getInstance(Config.class);
			config.initialize(injector);
			config.addModelSets(modelType.getModelSets());
			config.setClassPath(getPath().toString());
			decoratorModule.setConfig(config);
			
			// Add builders
			logger.debug("Creating builders");
			List<IBuilder> builders = new ArrayList<IBuilder>();
			for (BuilderTask builderTask : builderTasks) {
				builders.add(injector.getInstance(BuilderFctory.class).createBuilder(builderTask, config));
			}
			
			// Create and run processor
			logger.debug("Generating");
			JacobProcessor processor = injector.getInstance(JacobProcessor.class);
			processor.process(builders);
			
			logger.debug("Done");
			
		} catch (RuntimeException re) {
			re.printStackTrace();
		} finally {
			// reset loader
			resetThreadContextLoader();
		}
	}

	/**
	 * Validate all task configuration before execute
	 * @throws BuildException
	 */
	protected void validateConfiguration() throws BuildException {
		
		// validate model configuration
		if (modelType == null) {
			throw new BuildException("No model defined.");
		} else {
			modelType.validateConfiguration();
		}
		
		// validate builders
		if (builderTasks.isEmpty()) {
			throw new BuildException("No builders defined.");
		} else {
			for (BuilderTask builderTask : builderTasks) {
				builderTask.validateConfiguration();
			}
		}
		
	}
	
	/* Empty module */
	private final static Module emptyModule = new Module() { public void configure(Binder binder) { }};

	private final static Logger logger = Logger.getLogger(JacobTask.class);
	
}
